package org.example.user.statistic.controller;

import org.example.commons.utils.DateUtils;
import org.example.commons.vo.ResultVO;
import org.example.user.statistic.service.ISysHealthRecordService;
import org.example.user.manage.service.ISysHealthStatusService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * author 陈伯瑞
 * date 2020/6/15 0015 21:54
 * version 1.0
 **/
@RestController
    @RequestMapping("/api/health")
public class HealthAnalysis {
    /**
     *出入记录和健康状态记录数据分析
     *
     */
    @Autowired
    private ISysHealthRecordService healthRecordService;
    @Autowired
    private ISysHealthStatusService healthStatusService;

    @GetMapping("/recordNum")
    public ResultVO analysis()  {
        return  ResultVO.success(healthRecordService.queryAll().size());
    }

    @GetMapping("/statusNum")
    public ResultVO analy()  {
        return  ResultVO.success(healthStatusService.queryAll().size());
    }

    /*返回最近三十天和上个三十天的数据*/
    @GetMapping("/recordBymonth")
    public ResultVO recordBymonth() {
        Map map=new HashMap();
        List<Integer> data = new ArrayList<Integer>();
        data.add(healthRecordService.queryLastRecordBydays(24).size());
        data.add(healthRecordService.queryLastRecordBydays(18).size());
        data.add(healthRecordService.queryLastRecordBydays(12).size());
        data.add(healthRecordService.queryLastRecordBydays(6).size());
        data.add(healthRecordService.queryLastRecordBydays(0).size());
        map.put("newData",data);

        List<Integer> datal = new ArrayList<Integer>();
        datal.add(healthRecordService.queryLastRecordBydays(49).size());
        datal.add(healthRecordService.queryLastRecordBydays(43).size());
        datal.add(healthRecordService.queryLastRecordBydays(37).size());
        datal.add(healthRecordService.queryLastRecordBydays(31).size());
        datal.add(healthRecordService.queryLastRecordBydays(25).size());
        map.put("oldData",datal);
        return ResultVO.success(map);
    }


    @GetMapping("/statusBymonth")
    public ResultVO statusBymonth() {
        Map map=new HashMap();
        List<Integer> data = new ArrayList<Integer>();
        data.add(healthStatusService.queryLastStatusBydays(24).size());
        data.add(healthStatusService.queryLastStatusBydays(18).size());
        data.add(healthStatusService.queryLastStatusBydays(12).size());
        data.add(healthStatusService.queryLastStatusBydays(6).size());
        data.add(healthStatusService.queryLastStatusBydays(0).size());
        map.put("newData",data);

        List<Integer> datal = new ArrayList<Integer>();
        datal.add(healthStatusService.queryLastStatusBydays(49).size());
        datal.add(healthStatusService.queryLastStatusBydays(43).size());
        datal.add(healthStatusService.queryLastStatusBydays(31).size());
        datal.add(healthStatusService.queryLastStatusBydays(25).size());
        map.put("oldData",datal);
        return ResultVO.success(map);
    }

    /*返回用户最近五次扫码的体温*/
    @GetMapping("/statusChange")
    public ResultVO statusChange(@RequestParam(name = "userId") Long userid) {
        List<Float>  sysRecordTemperature = healthRecordService.queryLastTimesUser(userid, 5);
        List<Date>  sysRecord= healthRecordService.findLastTimesUserRecord(userid, 5);
        List<String> Record = new ArrayList<>();
        sysRecord.forEach(item->{Record.add(DateUtils.dateTime(item));});
        Map map=new HashMap();
        map.put("title",Record);
        map.put("data",sysRecordTemperature);
        return ResultVO.success(map);
    }

    /*返回用户某时间段体温平均值*/
    @GetMapping("/averageTemperature")
    public ResultVO averageTemperature() {
        List<String> title = new ArrayList<String>();
        List<Float> avgTemperature = new ArrayList<Float>();
        title.add("24-28天");
        title.add("20-24天");
        title.add("16-20天");
        title.add("12-16天");
        title.add("8-12天");
        title.add("4-8天");
        title.add("过去0-4天");
        avgTemperature.add(healthRecordService.findTimeAvgTemperature(24,28));
        avgTemperature.add(healthRecordService.findTimeAvgTemperature(20,24));
        avgTemperature.add(healthRecordService.findTimeAvgTemperature(16,20));
        avgTemperature.add(healthRecordService.findTimeAvgTemperature(12,16));
        avgTemperature.add(healthRecordService.findTimeAvgTemperature(8,12));
        avgTemperature.add(healthRecordService.findTimeAvgTemperature(4,8));
        avgTemperature.add(healthRecordService.findTimeAvgTemperature(0,4));
        Map map=new HashMap();
        map.put("title",title);
        map.put("data",avgTemperature);
        return ResultVO.success(map);
    }

    /*返回用户某时间段出入总数*/
    @GetMapping("/recordSum")
    public ResultVO recordSum() {
        List<String> title = new ArrayList<String>();
        List<Long> recordSum = new ArrayList<Long>();
        title.add("30-35天");
        title.add("25-30天");
        title.add("20-25天");
        title.add("15-20天");
        title.add("10-15天");
        title.add("5-10天");
        title.add("0-5天  最近");
        recordSum.add(healthRecordService.findTimeSum(30,35));
        recordSum.add(healthRecordService.findTimeSum(25,30));
        recordSum.add(healthRecordService.findTimeSum(20,25));
        recordSum.add(healthRecordService.findTimeSum(15,20));
        recordSum.add(healthRecordService.findTimeSum(10,15));
        recordSum.add(healthRecordService.findTimeSum(5,10));
        recordSum.add(healthRecordService.findTimeSum(0,5));
        Map map=new HashMap();
        map.put("title",title);
        map.put("data",recordSum);
        return ResultVO.success(map);
    }

}
